package DAO;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import Classes.Cliente;
import Classes.Endereco;
import Nucleo.ConexaoBanco;

public class ClienteDAO {

	private ConexaoBanco conn;
	
	public ClienteDAO(){
		conn = new ConexaoBanco();		
	}

	public void inserir( Cliente cliente ){
		String sql = "INSERT INTO `Cliente` (nome,telefone,dt_cadastro) values ( ? , ? , ?) ";
		try {
			PreparedStatement stmt = conn.getConnection().prepareStatement(sql);
			stmt.setString(1, cliente.getNome() );
			stmt.setString(2, cliente.getTelefone() );
			stmt.setString(3, cliente.getDataCadastro() );
			stmt.execute();
			stmt.close();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public List<Cliente> getClientes(){
		String sql = "SELECT * FROM `Cliente`";
		List<Cliente> lista = new ArrayList<Cliente>();
		
		try {
			PreparedStatement stmt = conn.getConnection().prepareStatement(sql);
			ResultSet rs = stmt.executeQuery();
						
			while(rs.next()) {
				SimpleDateFormat formatarDate = new SimpleDateFormat("yyyy-MM-dd");
			
				Cliente cliente = new Cliente();	
				
				cliente.setId	( rs.getInt("idCliente") );
				cliente.setNome( rs.getString("nome") );
				cliente.setTelefone( rs.getString("telefone") );
				cliente.setDataCadastro( formatarDate.format(rs.getDate("dt_cadastro"))  );
				
				EnderecoDAO enderecoDao = new EnderecoDAO();
				Endereco endereco = enderecoDao.getEnderecoDoCliente(cliente.getId()); 
				
				cliente.setEndereco(endereco);
				
				lista.add( cliente );
			}
			
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return lista;		
	}
	
	public Cliente getClientePeloId( int idCliente ){
		String sql = "SELECT * FROM `Cliente` WHERE `idCliente` = '"+ idCliente+"'";
		Cliente cliente = null;
		
		try {
			PreparedStatement stmt = conn.getConnection().prepareStatement(sql);
			ResultSet rs = stmt.executeQuery();
			
			while(rs.next()) {
				SimpleDateFormat formatarDate = new SimpleDateFormat("yyyy-MM-dd");
				
				cliente = new Cliente();	
				
				cliente.setId	( rs.getInt("idCliente") );
				cliente.setNome( rs.getString("nome") );
				cliente.setTelefone( rs.getString("telefone") );
				cliente.setDataCadastro( formatarDate.format(rs.getDate("dt_cadastro"))  );
				
				EnderecoDAO enderecoDao = new EnderecoDAO();
				Endereco endereco = enderecoDao.getEnderecoDoCliente(cliente.getId()); 
				
				cliente.setEndereco(endereco);
				
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return cliente;
	}

	public boolean deletar( int idCliente ) {
		new EnderecoDAO().deletar(idCliente);

		String sql = "DELETE FROM `Cliente` WHERE `idCliente` = '"+ idCliente+"'";
		
		try {
			Statement stmt = conn.getConnection().createStatement();
			int delete = stmt.executeUpdate(sql);
			if ( delete == 1) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
		
		
		
	}
}
